package com.zkingsoft.services.sys.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.authority.Matrix;
import com.zkingsoft.dao.sys.SysRoleDao;
import com.zkingsoft.dao.sys.SysRolePwoerFnDao;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.sys.SysFunction;
import com.zkingsoft.model.sys.SysRole;
import com.zkingsoft.model.sys.SysRolePwoerFn;
import com.zkingsoft.model.sys.SysUsers;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.sys.SysRoleService;
import com.zkingsoft.util.ModelUtils;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-11-16 15:25
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysRolePwoerFnDao rolePwoerDao;
	@Autowired
	private Matrix matrix;

	@Transactional
	@Override
	public int add(SysRole sysRole) {
		SysUsers user = matrix.getLoginUser();
		sysRole.setCompanyId(user.getCompanyId());
		// 新增角色对应的中间表
		int i = sysRoleDao.insert(sysRole);
		addRolePwoer(sysRole);
		return i;

	}

	/**
	 * 新增角色的权限，添加前清空已有的权限
	 * 
	 * @param sysRole
	 */
	private void addRolePwoer(SysRole sysRole) {
		SysRolePwoerFn sysRolePwoerFn = new SysRolePwoerFn();
		sysRolePwoerFn.setRoleId(sysRole.getRoleId());
		rolePwoerDao.deleteByModel(sysRolePwoerFn);
		List<SysFunction> fnList = sysRole.getFnList();
		List<SysRolePwoerFn> rolePwoer = new ArrayList<>();
		if (fnList != null) {
			for (SysFunction sysFunction : fnList) {
				// 如果为空则不在进行添加
				if (sysFunction == null || sysFunction.getFnId() == null) {
					continue;
				}
				SysRolePwoerFn temp = new SysRolePwoerFn();
				temp.setFnId(sysFunction.getFnId());
				temp.setRoleId(sysRole.getRoleId());
				temp.setRpfBtns(sysFunction.getFnBtns() == null ? "" : sysFunction.getFnBtns());
				rolePwoer.add(temp);
			}
		}
		if (rolePwoer.size() > 0) {
			rolePwoerDao.batchInsert(rolePwoer);
		}
	}

	@Override
	public int batchAdd(List<SysRole> sysRoleList) {
		return sysRoleDao.batchInsert(sysRoleList);
	}

	@Override
	public int modifyByMap(SysRole oldSysRole, SysRole newSysRole) {
		// 这样写只能保证基本信息不被覆盖
		addRolePwoer(newSysRole);
		if (!ModelUtils.isModified(oldSysRole, newSysRole)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldSysRole, newSysRole);
		} catch (Exception e) {
			new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("roleId", oldSysRole.getRoleId());
			sysRoleDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(SysRole sysRole) {

		return sysRoleDao.updateByModel(sysRole);

	}

	@Override
	public int remove(List<Long> list) {

		return sysRoleDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long roleId) {

		return sysRoleDao.deleteById(roleId);

	}

	@Override
	public int removeByModel(SysRole sysRole) {

		return sysRoleDao.deleteByModel(sysRole);

	}

	@Override
	public List<SysRole> findInPage(SysRole sysRole, PaginationVO pageVo) {

		return sysRoleDao.selectInPage(sysRole, pageVo);

	}

	@Override
	public List<SysRole> findByModel(SysRole sysRole) {

		return sysRoleDao.selectByModel(sysRole);

	}

	@Override
	public int findTotal(SysRole sysRole) {

		return sysRoleDao.selectTotalRecord(sysRole);

	}

	@Override
	public SysRole findById(Long roleId) {

		return sysRoleDao.selectById(roleId);

	}

}
